import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { DialogQueue } from '@/utils/dialogQueue'
import { appoxEnums } from '@/appox/appox'
import { isDark } from '@/utils/utility'

export const useTaskBannerStore = defineStore('taskBanner', () => {
  // 状态
  const dialogQueue = new DialogQueue()
  const currentDialog = ref(null)
  const progress = ref(0)
  const state = ref(appoxEnums.TaskState.Unknown)
  const description = ref('')
  const checkDetails = ref(false)
  const isCancellable = ref(true)
  const taskCallback = ref(null)
  let timer = null  // 添加timer变量

  // 计算属性
  const progressText = computed(() => {
    const value = Math.max(0, Math.min(100, progress.value))
    return `${value.toFixed(1)}%`
  })

  const progressColor = computed(() => {
    switch (state.value) {
      case appoxEnums.TaskState.Unknown:
        return 'gray'
      case appoxEnums.TaskState.Progressing:
        return isDark ? 'white' : 'black'
      case appoxEnums.TaskState.Failed:
        return 'red'
      case appoxEnums.TaskState.Finished:
        return 'green'
      default:
        return isDark ? 'white' : 'black'
    }
  })

  // 动作
  function initialize(taskTitle, callback) {
    currentDialog.value = {
      title: taskTitle
    }
    progress.value = 0
    state.value = appoxEnums.TaskState.Unknown
    description.value = ''
    checkDetails.value = false
    isCancellable.value = true
    taskCallback.value = callback
  }

  function updateState() {
    if (!taskCallback.value) return

    const newState = taskCallback.value.getTaskState()
    const newDescription = taskCallback.value.getTaskStateDescription()
    const newProgress = taskCallback.value.getTaskProgress()

    state.value = newState
    description.value = newDescription || ''
    progress.value = newProgress

    // 检查任务是否完成
    if (state.value === appoxEnums.TaskState.Failed || state.value === appoxEnums.TaskState.Finished) {
      isCancellable.value = false
      setTimeout(() => {
        hide()
      }, 2000)
    }
  }

  async function cancelTask() {
    if (!isCancellable.value || !taskCallback.value) return
    
    isCancellable.value = false
    await taskCallback.value.cancelTask()
    hide()
  }

  function toggleCheckDetails() {
    checkDetails.value = !checkDetails.value
  }

  function show(taskTitle, callback) {
    // 清除之前的定时器
    if (timer) {
      clearInterval(timer)
      timer = null
    }
    
    return dialogQueue.show({
      onLoaded: () => {
        initialize(taskTitle, callback)
        // 设置定时器更新状态
        timer = setInterval(updateState, 100)
      },
      onHidden: () => {
        if (timer) {
          clearInterval(timer)
          timer = null
        }
        return checkDetails.value
      }
    })
  }

  function hide() {
    dialogQueue.hide(checkDetails.value)
  }

  return {
    // 状态
    isVisible: dialogQueue.isVisible,
    title: () => currentDialog.value?.title || '',
    progress,
    state,
    description,
    checkDetails,
    isCancellable,
    // 计算属性
    progressText,
    progressColor,
    // 动作
    cancelTask,
    toggleCheckDetails,
    show,
    hide,
    getQueueLength: dialogQueue.getQueueLength.bind(dialogQueue),
    clearQueue: dialogQueue.clearQueue.bind(dialogQueue)
  }
})